﻿using System.Linq;
using NPoco;
using Blog.Core.Entity;

namespace Blog.Core.Service
{
    public class PostService : ServiceBase
    {
        public Page<PostInfo> GetPostList(long pageIndex, long pageSize, int categoryId)
        {
            var sqlBuilder = new SqlBuilder();
            var sql = sqlBuilder.AddTemplate("select a.*,b.CategoryName from Post a,PostCategory b where a.CategoryId=b.Id and /**where**/ /**orderby**/");
            if (categoryId != 0)
            {
                sqlBuilder.Where("a.CategoryId=@0", categoryId);
            }
            sqlBuilder.OrderBy("a.Id Desc");
            return DBHelper.Page<PostInfo>(pageIndex, pageSize, sql);
        }

        public PostInfo GetPostInfo(int id)
        {
            var sqlBuilder = new SqlBuilder();
            var sql = sqlBuilder.AddTemplate("select a.*,b.CategoryName from Post a,PostCategory b where a.CategoryId=b.Id and a.Id=@0 /**orderby**/", id);
            sqlBuilder.OrderBy("a.Id Desc");

            return DBHelper.Fetch<PostInfo>(sql).FirstOrDefault();
        }
    }
}
